package com.wonderful.eyepetizer.ui.pages.detail

import android.app.Activity
import android.content.Context
import android.content.Intent
import android.os.Bundle
import androidx.activity.ComponentActivity
import androidx.activity.compose.setContent
import androidx.compose.foundation.Image
import androidx.compose.foundation.background
import androidx.compose.foundation.border
import androidx.compose.foundation.layout.Box
import androidx.compose.foundation.layout.Column
import androidx.compose.foundation.layout.Spacer
import androidx.compose.foundation.layout.fillMaxWidth
import androidx.compose.foundation.layout.height
import androidx.compose.foundation.layout.padding
import androidx.compose.foundation.layout.wrapContentHeight
import androidx.compose.foundation.lazy.LazyColumn
import androidx.compose.foundation.lazy.items
import androidx.compose.foundation.shape.RoundedCornerShape
import androidx.compose.material.ExperimentalMaterialApi
import androidx.compose.material.Text
import androidx.compose.material.pullrefresh.PullRefreshIndicator
import androidx.compose.material.pullrefresh.pullRefresh
import androidx.compose.material.pullrefresh.rememberPullRefreshState
import androidx.compose.runtime.Composable
import androidx.compose.runtime.LaunchedEffect
import androidx.compose.ui.Alignment
import androidx.compose.ui.Modifier
import androidx.compose.ui.draw.clip
import androidx.compose.ui.graphics.Color
import androidx.compose.ui.layout.ContentScale
import androidx.compose.ui.platform.LocalContext
import androidx.compose.ui.text.TextStyle
import androidx.compose.ui.text.font.FontWeight
import androidx.compose.ui.unit.dp
import androidx.compose.ui.unit.sp
import androidx.compose.ui.zIndex
import androidx.core.view.WindowInsetsControllerCompat
import androidx.lifecycle.viewmodel.compose.viewModel
import coil.compose.rememberAsyncImagePainter
import com.wonderful.eyepetizer.data.entity.bean.TopicDetail
import com.wonderful.eyepetizer.ui.pages.daily.RankItemWidget
import com.wonderful.eyepetizer.ui.pages.detail.viewmodel.TopicDetailViewModel
import com.wonderful.eyepetizer.ui.theme.Black_12
import com.wonderful.eyepetizer.ui.theme.Border_bg
import com.wonderful.eyepetizer.ui.theme.NormalItemColor
import com.wonderful.eyepetizer.view.TitleBarWidget
import dagger.hilt.android.AndroidEntryPoint

/**
 * Desc:
 * @Author: wonderful
 * @Time: 2024/2/22 16:42
 */
@AndroidEntryPoint
class TopicDetailActivity : ComponentActivity() {
    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        window.statusBarColor = android.graphics.Color.WHITE
        WindowInsetsControllerCompat(window, window.decorView).apply {
            isAppearanceLightStatusBars = true
        }
        val topicId = intent.getIntExtra(KEY_TOPIC_ID, 0)
        setContent {
            TopicDetailPage(topicId = topicId)
        }
    }

    companion object {
        private const val KEY_TOPIC_ID = "topic_id"
        fun actionStart(context: Context, topicId: Int) {
            val intent = Intent(context, TopicDetailActivity::class.java)
            intent.putExtra(KEY_TOPIC_ID, topicId)
            context.startActivity(intent)
        }
    }
}

@OptIn(ExperimentalMaterialApi::class)
@Composable
fun TopicDetailPage(topicId: Int) {
    val context = LocalContext.current
    val viewModel: TopicDetailViewModel = viewModel()
    LaunchedEffect(key1 = Unit, block = {
        viewModel.getTopicDetail(topicId)
    })
    val pullRefreshState = rememberPullRefreshState(
        refreshing = viewModel.refreshing.value,
        onRefresh = { viewModel.getTopicDetail(topicId) })
    val topicDetail = viewModel.topicDetail.value
    Column {
        TitleBarWidget(title = topicDetail.brief, showNavigationIcon = true, onBack = {
            if (context is Activity) context.finish()
        })
        Box(
            modifier = Modifier
                .fillMaxWidth()
                .weight(1f)
                .zIndex(-1f)
                .pullRefresh(pullRefreshState)
        ) {
            LazyColumn(content = {
                item {
                    TopicDetailHeadWidget(topicDetail = topicDetail)
                }
                items(topicDetail.itemList) {
                    it.data.content.data?.run {
                        RankItemWidget(itemData = this)
                    }
                }
            })
            PullRefreshIndicator(
                refreshing = viewModel.refreshing.value,
                state = pullRefreshState,
                modifier = Modifier.align(Alignment.TopCenter)
            )
        }
    }
}

@Composable
fun TopicDetailHeadWidget(topicDetail: TopicDetail) {
    Box(
        modifier = Modifier
            .fillMaxWidth()
            .wrapContentHeight()
    ) {
        Column(modifier = Modifier.fillMaxWidth()) {
            Image(
                painter = rememberAsyncImagePainter(model = topicDetail.headerImage),
                contentDescription = null,
                modifier = Modifier
                    .fillMaxWidth()
                    .height(250.dp),
                contentScale = ContentScale.Crop
            )
            Text(
                text = topicDetail.text,
                style = TextStyle(fontSize = 12.sp, color = NormalItemColor),
                modifier = Modifier.padding(start = 12.dp, top = 40.dp, end = 20.dp, bottom = 10.dp)
            )
            Spacer(
                modifier = Modifier
                    .fillMaxWidth()
                    .height(5.dp)
                    .background(Black_12)
            )
        }
        Box(
            modifier = Modifier
                .fillMaxWidth()
                .padding(start = 20.dp, end = 20.dp, top = 230.dp)
                .height(50.dp)
                .clip(RoundedCornerShape(4.dp))
                .background(Color.White)
                .border(1.dp, Border_bg),
            contentAlignment = Alignment.Center
        ) {
            Text(
                text = topicDetail.brief,
                style = TextStyle(
                    fontSize = 14.sp,
                    fontWeight = FontWeight.Bold,
                    color = Color.Black
                )
            )
        }
    }
}